package com.carey.swift.dev.servlet;

import com.carey.swift.dev.mapping.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

public class MainServlet extends HttpServlet {
 private final static Logger logger = LoggerFactory.getLogger(MainServlet.class);


    private static final String SCAN="com.test.controller";

    private static final SwiftHandlerMethod swiftHandlerMethod =new SwiftHandlerMethod();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        swiftHandlerMethod.processRequest(req,resp);
    }

    @Override
    protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        swiftHandlerMethod.processRequest(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        swiftHandlerMethod.processRequest(req,resp);
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        swiftHandlerMethod.processRequest(req,resp);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        swiftHandlerMethod.processRequest(req,resp);
    }

    @Override
    protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        swiftHandlerMethod.processRequest(req,resp);
    }

    @Override
    protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        swiftHandlerMethod.processRequest(req,resp);
    }


    @Override
    public void init() throws ServletException {

        logger.info("控制器加载中..");
        String path = this.getClass().getResource("/").getPath();
        logger.info("扫描路径：{}",SCAN);

        String scanPath= SCAN.replaceAll("\\.","/");

        File file =new File(path+scanPath);
        List<String> clazzList= Arrays.asList(file.list());

        for(String clazz: clazzList){
            clazz= "." + clazz.replaceAll("\\.class","");
            try {
                Object o= Class.forName(SCAN + clazz).newInstance();
                String controllerUrl="";
                if(o.getClass().isAnnotationPresent(Controller.class)){
                    if(o.getClass().isAnnotationPresent(WebMapping.class)){
                       WebMapping webMapping=  o.getClass().getDeclaredAnnotation(WebMapping.class);
                       controllerUrl= webMapping.url();
                       if(controllerUrl.equals("/")){
                           controllerUrl = "";
                       }

                       List<Method> methodList= Arrays.asList(o.getClass().getDeclaredMethods());
                       for(Method method: methodList){
                           if(method.isAnnotationPresent(WebMapping.class)){
                               webMapping=  method.getDeclaredAnnotation(WebMapping.class);
                               String url= webMapping.url();
                               HttpMethod httpMethod= webMapping.method();
                               ControllerModel controllerModel=new ControllerModel();
                               controllerModel.setHttpMethod(httpMethod);
                               controllerModel.setObject(o);
                               controllerModel.setMethod(method);
                               logger.info("Controller init ：[ {} ,{}, MethodType: {} ]",controllerUrl + url,method,httpMethod);
                               StaticCache.setObjectMap(controllerUrl + url,controllerModel);

                           }
                       }
                        logger.info("初始化 done..");
                    }
                }
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        super.init();
    }
}
